package com.docmgmt.mvp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.docmgmt.mvp.entity.KbFileMappingEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 知识库文件映射Mapper
 */
@Mapper
public interface KbFileMappingMapper extends BaseMapper<KbFileMappingEntity> {

    /**
     * 查询知识库中的所有文件
     */
    @Select("SELECT * FROM kb_file_mapping WHERE kb_id = #{kbId} AND file_status = 'active' AND deleted_at IS NULL")
    List<KbFileMappingEntity> selectActiveFilesByKbId(@Param("kbId") Long kbId);

    /**
     * 查询文件所属的所有知识库
     */
    @Select("SELECT * FROM kb_file_mapping WHERE file_id = #{fileId} AND file_status = 'active' AND deleted_at IS NULL")
    List<KbFileMappingEntity> selectKbsByFileId(@Param("fileId") Long fileId);

    /**
     * 检查文件是否已在知识库中
     */
    @Select("SELECT COUNT(*) FROM kb_file_mapping WHERE kb_id = #{kbId} AND file_id = #{fileId} AND file_status = 'active' AND deleted_at IS NULL")
    int existsInKb(@Param("kbId") Long kbId, @Param("fileId") Long fileId);
}
